//
//  HotDetailsTableViewCell.m
//  SugarPile
//
//  Created by Charlie. on 16/1/21.
//  Copyright © 2016年 www.lanou39.蓝鸥科技. All rights reserved.
//

#import "HotDetailsTableViewCell.h"

@interface HotDetailsTableViewCell ()

@property (nonatomic, retain) UIImageView *pathView;/**< 图片 */
@property (nonatomic, retain) UIImageView *avatarView; /**<用户名头像*/
@property (nonatomic, retain) UILabel *userNameLabel; /**<用户名*/
@property (nonatomic, retain) UILabel *nameLabel; /**<收集的专辑*/
@property (nonatomic, retain) UILabel *add_datetimeLabel; /**<里发布的日期*/
@property (nonatomic, retain) UIView *lineView; /**<分割线*/
@property (nonatomic, retain) UILabel *msgLaber; /**<自适应文本*/

@property (nonatomic, retain) UIImageView *godUserNameLabel; /**<点赞用户头像*/
@property (nonatomic, retain) UILabel *godLabel; /**<点赞的文字*/
@property (nonatomic, retain) UIView *godLineView; /**<god线*/

@property (nonatomic, retain) UILabel *comLabel; /**< 评论文字 */
@property (nonatomic, assign) NSInteger i; /**<评论记录动态高度*/

@property (nonatomic, assign) NSInteger number; /**< 记录点击的按钮 */
@end
@implementation HotDetailsTableViewCell

- (void)dealloc
{
    [_pathView release];
    [_dierView release];
    [_avatarView release];
    [_userNameLabel release];
    [_nameLabel release];
    [_add_datetimeLabel release];
    [_dataDic release];
    [_godLabel release];
    [_godUserNameLabel release];
    [_godView release];
    [_godLineView release];
    [_comView release];
    [_comLabel release];
    [_comButton release];
    [super dealloc];
}
- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        // 底面
        self.dierView = [[UIImageView alloc] init];
        _dierView.backgroundColor = [UIColor floralWhite];
        [self.contentView addSubview:_dierView];
        [self.dierView release];
        
        // 图片
        self.pathView = [[UIImageView alloc] init];
//        _pathView.backgroundColor = [UIColor yellowColor];
        [self.contentView addSubview:_pathView];
        [self.pathView release];
        
        // 用户名头像
        self.avatarView = [[UIImageView alloc] init];
//        _avatarView.backgroundColor = [UIColor redColor];
        [self.contentView addSubview:_avatarView];
        [self.avatarView release];
        
        // 用户名
        self.userNameLabel = [[UILabel alloc] init];
//        _userNameLabel.backgroundColor = [UIColor redColor];
        _userNameLabel.textColor = [UIColor blueColor];
        [self.contentView addSubview: _userNameLabel];
        [self.userNameLabel release];
        
        // 收藏的专辑
        self.nameLabel = [[UILabel alloc] init];
//        _nameLabel.backgroundColor = [UIColor blueColor];
        [self.contentView addSubview:_nameLabel];
        _nameLabel.font = [UIFont systemFontOfSize:15];
        [self.nameLabel release];
        
        // 发布的日期
        self.add_datetimeLabel = [[UILabel alloc] init];
//        _add_datetimeLabel.backgroundColor = [UIColor grayColor];
        _add_datetimeLabel.font = [UIFont systemFontOfSize:9];
        [self.contentView addSubview:_add_datetimeLabel];
        [self.add_datetimeLabel release];
        
        // 分割线
        self.lineView = [[UIView alloc] init];
        _lineView.backgroundColor = [UIColor lightGrayColor];
        [self.contentView addSubview:_lineView];
        [self.lineView release];
        
        // 自适应文本
        
        self.msgLaber = [[UILabel alloc] init];
//        _msgLaber.backgroundColor = [UIColor magentaColor];
        [self.contentView addSubview:_msgLaber];
        _msgLaber.font = [UIFont systemFontOfSize:15];
        _msgLaber.numberOfLines = 0;
        [self.msgLaber release];
        
        // God底面
        self.godView = [[UIView alloc] init];
        [self.contentView addSubview:_godView];
        _godView.backgroundColor = [UIColor floralWhite];
        [self.godView release];
        
        // god 文字
        self.godLabel = [[UILabel alloc] init];
        [self.godView addSubview:_godLabel];
//        self.godLabel.backgroundColor = [UIColor silverColor];
//        _godLabel.font = [UIFont systemFontOfSize:17];
        [self.godLabel release];
        
        // god线
        self.godLineView = [[UIView alloc] init];
        [self.godView addSubview:_godLineView];
        self.godLineView.backgroundColor = [UIColor lightGrayColor];
        [self.godLineView release];
        
        // com底面
        self.comView = [[UIView alloc] init];
        [self.contentView addSubview:_comView];
        self.comView.backgroundColor = [UIColor floralWhite];
        [self.comView release];
        
        // com评论文字
        self.comLabel = [[UILabel alloc] init];
        [self.comView addSubview:_comLabel];
//        self.comLabel.backgroundColor = [UIColor bronzeColor];
        [self.comLabel release];
        
        // god评论图片
        self.godButtonImage = [[UIImageView alloc] init];
        [_godButtonImage release];
        
//        // layout collection 初始化
//        self.layout = [[UICollectionViewFlowLayout alloc] init];
//        [self.layout release];
//        self.userNameCollection = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, 55 * H) collectionViewLayout:_layout];
    }
    return self;
}
- (void)setDataDic:(NSDictionary *)dataDic
{
    if (_dataDic != dataDic) {
        [_dataDic release];
        _dataDic = [dataDic retain];
        
        // 图片自适应 宽高比
        // _dataDic[@"photo"][@"height"] 取到字典中 图片高属性
        // _dataDic[@"photo"][@"width"] 取到字典 图片的宽度
        CGFloat f = [_dataDic[@"photo"][@"height"] floatValue] / [_dataDic[@"photo"][@"width"] floatValue];
        _pathView.frame = CGRectMake(10, 10, 355 * W, (355 * f) * H);
        [_pathView sd_setImageWithURL:[NSURL URLWithString:[_dataDic[@"photo"][@"path"]stringByReplacingOccurrencesOfString:@"_webp" withString:@""]] placeholderImage:[UIImage imageNamed:@"zhan"]];
        
        // 用户名
//        _dataDic[@"sender"][@"username"] 取到字典 用户名的Label
        CGFloat userNameW = [[self class] widthWithText:(_dataDic[@"sender"][@"username"]) Size:17];
        _userNameLabel.frame = CGRectMake(80 * W, _pathView.bounds.size.height + 20 * H, userNameW, 25 * H);
        _userNameLabel.text = _dataDic[@"sender"][@"username"];
        // 离发布的日期
//        
        _add_datetimeLabel.frame = CGRectMake(80 * W + userNameW, _pathView.bounds.size.height + 20 * H, 40 * W, 25 * H);
        _add_datetimeLabel.text = [@" · " stringByAppendingString:_dataDic[@"add_datetime_pretty"]];
        
        // 用户名头像
        _avatarView.frame = CGRectMake(20 * W, _pathView.bounds.size.height + 20 * H, 50 * W, 50 * W);
        _avatarView.layer.cornerRadius = _avatarView.bounds.size.width / 2;
        _avatarView.clipsToBounds = YES;
        [_avatarView sd_setImageWithURL:dataDic[@"sender"][@"avatar"] placeholderImage:[UIImage imageNamed:@"zhan"]];
        
        // 收集的专辑
        NSString *name = [@"收集到 " stringByAppendingString:_dataDic[@"album"][@"name"]];
        CGFloat nameW = [[self class] widthWithText:name Size:15];
        _nameLabel.frame = CGRectMake(80 * W, _pathView.bounds.size.height + 45 * H , nameW, 25 * H);
        _nameLabel.text = name;
        
        // 分割线
        self.lineView.frame = CGRectMake(20 * W, _pathView.bounds.size.height + 85 * H, WIDTH - 30 * W, 1);
        
        // 主文本
        CGFloat msgH = [[self class] heightWithText:_dataDic[@"msg"] Size:15];
        self.msgLaber.frame = CGRectMake(20 * W, _pathView.bounds.size.height + 105 * H, WIDTH - 40 * W, msgH);
        self.msgLaber.text = _dataDic[@"msg"];
        
#pragma mark - 主底面
        self.dierView.frame = CGRectMake(10 * W, 10 * H, WIDTH - 20 * W , _pathView.bounds.size.height + 105 * W + msgH);
        //<<<<<<<<<<<<<<<<<<<<<<<<< 赞 >>>>>>>>>>>>>>>>>>>>>>>>>>>>//
        // 解析点赞的人数
        NSArray *likeArr = [HotDetailsModel modelHanderWithArray:_dataDic[@"top_like_users"]];
//        NSLog(@"%@", likeArr);
        // 把点赞的用户的头像以及赞以及线 主视图设置在 godView 上
#pragma mark - 赞底面
        self.godView.frame = CGRectMake(10 * W, _dierView.frame.size.height + 30 * H, WIDTH - 20 * W, 110 * H);
        // 判断是否有点赞的人 如果为空 则隐藏GodView (godView高度为0)
        if ([likeArr lastObject] == nil) {
            _godView.frame = CGRectMake(0, 0, 0, 0);
        }else{
            
            // 赞
            self.godLabel.frame = CGRectMake(20 * W, 0, WIDTH - 60 * W, 45 * H);
            self.godLabel.text = [@"赞 " stringByAppendingString:[NSString stringWithFormat:@"%ld", (unsigned long)likeArr.count]];
            
            // 线
            self.godLineView.frame = CGRectMake(10 * W, 44 * H, WIDTH - 40 * W, 1);
            // 根据数组数量 循环创建并排的头像
            for (self.number = 0; _number < likeArr.count; _number++) {
                // 让视图只显示 5张 用户名 第6长用更多代替
                if (_number > 4) {
                    // 用户交互 替换 Button 点击跳转
                    // 创建一个更多 图片

                    _godButtonImage.frame = CGRectMake(20 * W + 55 * W * 5, 55 * H, 45 * W, 45 * W);
                    _godButtonImage.image = [UIImage imageNamed:@"gengduo"];
                    [self.godView addSubview:_godButtonImage];
                    // 添加godButton用户交互
                    _godButtonImage.userInteractionEnabled = YES;
                    self.godButton = [UIButton buttonWithType:UIButtonTypeSystem];
                    [self.godButtonImage addSubview:_godButton];
                    _godButton.frame = CGRectMake(0, 0, 45 * W, 45 * W);
                    _godButton.layer.cornerRadius = _godButton.frame.size.width / 2;
                    //                    image.backgroundColor = [UIColor whiteColor];
                    //                    image.layer.cornerRadius =image.frame.size.width / 2;
                    //                    image.clipsToBounds = YES;
                    // 内存释放
                    break;
                }
                // 设置tag值 覆盖Button 用户交互点击跳转
                // 创建 Model 用于方便赋值
                HotDetailsModel *m = likeArr[_number];
                UIImageView *image = [[UIImageView alloc] init];
                // 设置坐标
                image.frame = CGRectMake(20 * W + 55 * W * _number, 55 * H, 45 * W, 45 * W);
                // 设置圆角
                image.layer.cornerRadius =image.frame.size.width / 2;
                image.clipsToBounds = YES;
                // 放图片
                [image sd_setImageWithURL:[NSURL URLWithString:m.avatar] placeholderImage:[UIImage imageNamed:@"zhan"]];
                [self.godView addSubview:image];
                // 设置button
                UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
                // 设置tag值
                button.tag = 1000 + _number;
                // 坐标
                button.frame = image.frame;
                // 圆角
                button.layer.cornerRadius = button.frame.size.width / 2;
                button.clipsToBounds = YES;
//                button.backgroundColor = [UIColor seaGreen];
                [self.godView addSubview:button];
                // 用户交互
                button.userInteractionEnabled = YES;
                [button addTarget:self action:@selector(tian:) forControlEvents:UIControlEventTouchUpInside];
                NSLog(@"%@", m.userId);
                // 释放内存
                [image release];
            }
        }
        //<<<<<<<<<<<<<<<<<<<<<<<<< 评论 >>>>>>>>>>>>>>>>>>>>>>>>>>>>//
        self.i = 0; /**<让i 等于零*/
        // 解析评论的人数
        NSArray *comArr = [HotDetailsModel modelHanderWithArray:_dataDic[@"top_comments"][@"object_list"]];
        NSInteger j = [_dataDic[@"reply_count"] integerValue];
        // 判断是否写评论
        if (j == 0) {
            // 如果没有 则comView 高度为0;
            self.comView.frame = CGRectMake(0, 0, 0, 0);
        }else{
            
            // 赞
            self.comLabel.frame = CGRectMake(20 * W, 0, WIDTH - 60 * W, 44 * H);
            self.comLabel.text = [@"评论 " stringByAppendingString:[NSString stringWithFormat:@"%ld", j]];
            // 线
            UIView *line = [[UIView alloc] initWithFrame:CGRectMake(10 * W, 44 * H, WIDTH - 40 * W, 1)];
            line.backgroundColor = [UIColor lightGrayColor];
            [self.comView addSubview:line];
            [line release];
            
            for (NSInteger i = 0; i < j; i++) {
                // 判断是否大于2 如果大于2则增加一个查看更多
                if (i > 2) {
                    // 添加button
                    self.comButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
                    _comButton.frame = CGRectMake(WIDTH - 120 * W, 10 * H, 100 * W, 30 * H);
                    [_comButton setTitle:@"点击查看更多" forState:UIControlStateNormal];
                    [_comButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
//                    _comButton.backgroundColor = [UIColor bronzeColor];
                    [_comButton addTarget:self action:@selector(dian) forControlEvents:UIControlEventTouchUpInside];
                    [self.comView addSubview:_comButton];

                    break;
                }
                // 数据转换成Model
                HotDetailsModel *m = comArr[i];
                // 评论板
                UIView *View = [[UIView alloc] initWithFrame:CGRectMake(20 * W, 45 * H + 65 * H * i, WIDTH - 60 * W, 65 * H)];
//                View.backgroundColor = [UIColor goldColor];
                [self.comView addSubview:View];
                [View release];
                
                // 头像
                UIImageView *image = [[UIImageView alloc] initWithFrame:CGRectMake(0, 10 * H, 45 * W, 45 * W)];
                image.layer.cornerRadius =image.frame.size.width / 2;
                [image sd_setImageWithURL:[NSURL URLWithString:m.avatar] placeholderImage:[UIImage imageNamed:@"zhan"]];
                image.clipsToBounds = YES;
                [View addSubview:image];
                [image release];
                // 名字
                CGFloat h = [[self class] widthWithText:m.username Size:13];
                UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(55 * W, 10 * H, h, 22 * H)];
                label.text = m.username;
                label.font = [UIFont systemFontOfSize:13];
                [View addSubview:label];
                
                // 评论的文字
                // 设置自适应宽度
                UILabel *labelc = [[UILabel alloc] initWithFrame:CGRectMake(55 * W, 33 * H, WIDTH * 3 / 5, 22 * H)];
                // 设置文本内容
//                labelc.backgroundColor = [UIColor seaGreen];
                labelc.text = m.content;
                labelc.numberOfLines = 0;
                labelc.font = [UIFont systemFontOfSize:11];
                [View addSubview:labelc];
                
                // 评论的时间
                CGFloat ht = [[self class] widthWithText:m.add_datetime_pretty Size:9];
                UILabel *labelt = [[UILabel alloc] initWithFrame:CGRectMake(WIDTH - 100 * W, 10 * H, ht, 15 * H)];
                labelt.text = m.add_datetime_pretty;
                labelt.textColor = [UIColor lightGrayColor];
                labelt.font = [UIFont systemFontOfSize:9];
                [View addSubview:labelt];
                // 线
                UIView *line = [[UIView alloc] initWithFrame:CGRectMake(W * 10, H * 109  + 65 * H * i, WIDTH - 40 * W, 1)];
                line.backgroundColor = [UIColor lightGrayColor];
                [self.comView addSubview:line];
                [line release];

                // 记录 评论版高度
                self.i = 65 * (i + 1) * H;
            }
            // 评论底板的坐标 放到最后写 要适应 评论人数的高度
            if (_godView.bounds.size.height == 0) {
                _comView.frame = CGRectMake(10 * W, _dierView.bounds.size.height + _godView.bounds.size.height + 30 * H, WIDTH - 20 * W, self.i + 45 * H);
            }
            _comView.frame = CGRectMake(10 * W, _dierView.bounds.size.height + _godView.bounds.size.height + 50 * H, WIDTH - 20 * W, self.i + 45 * H);

        }
    }
}
+ (void)god
{
    
}
// 计算文本高度
+(CGFloat)heightWithText:(NSString *)text Size:(CGFloat)fontSize
{
    NSDictionary *dic = @{NSFontAttributeName: [UIFont systemFontOfSize:fontSize]};
    // 预设尺寸 文本最大不超过这个尺寸
    // 根据设置的宽度计算高度(宽度和label保持一致)
    CGSize size = CGSizeMake(WIDTH - 30, 1000);
    
    CGRect rect = [text boundingRectWithSize:size options:NSStringDrawingUsesLineFragmentOrigin attributes:dic context:nil];
    // 返回得到的高度
    
    return rect.size.height;
}

// 文本宽度
+ (CGFloat)widthWithText:(NSString *)text Size:(CGFloat)fontSize
{
    //保存文本属性
    NSDictionary *dic = @{NSFontAttributeName:[UIFont systemFontOfSize:fontSize]};
    //设置最大 尺寸
    //高度于label一致
    CGSize size = CGSizeMake(500, 10);
    //计算出 出一个 rect
    CGRect rect = [text boundingRectWithSize:size options:NSStringDrawingUsesLineFragmentOrigin attributes:dic context:nil];
    //返回得到的宽度
    return rect.size.width;
}

- (void)layoutSubviews
{
    [super layoutSubviews];
    
}
- (void)awakeFromNib {
    // Initialization code
}
// 实现cell中点击传值的方法
- (void)dian
{
    // 将要传的值给代理
    NSNumber *num = _dataDic[@"id"];
//    NSLog(@"%@", num);
    // 执行代理方法
    [self.delegte chuanWithNumber:num];
    
}
- (void)tian:(UIButton *)btn
{
    // 根据tag值来判断每个button进行传输的id
    if (btn.tag == 1000) {
        NSLog(@"%@", _dataDic[@"top_like_users"][0][@"id"]);
        [self.delegte userWithNumber:_dataDic[@"top_like_users"][0][@"id"]];
        
    }if (btn.tag == 1001){
        NSLog(@"%@", _dataDic[@"top_like_users"][1][@"id"]);
        [self.delegte userWithNumber:_dataDic[@"top_like_users"][1][@"id"]];
    }if (btn.tag == 1002) {
        NSLog(@"%@", _dataDic[@"top_like_users"][2][@"id"]);
        [self.delegte userWithNumber:_dataDic[@"top_like_users"][2][@"id"]];
    }if (btn.tag == 1003) {
        NSLog(@"%@", _dataDic[@"top_like_users"][3][@"id"]);
        [self.delegte userWithNumber:_dataDic[@"top_like_users"][3][@"id"]];
    }if (btn.tag == 1004) {
        NSLog(@"%@", _dataDic[@"top_like_users"][4][@"id"]);
        [self.delegte userWithNumber:_dataDic[@"top_like_users"][4][@"id"]];
    }
    
}
- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
